package com.salim.leetcode.$122;

public class BestTimeToBuyAndSellStockII {
    //找到所有上升趋势 相加
    public int maxProfit(int[] prices) {
        int minIndex = 0;
        int maxIndex = 0;
        int result = 0;
        for(int i=1;i<prices.length;i++){
            if(prices[i]>=prices[maxIndex]){
                maxIndex = i;
            }else if(prices[i]<prices[maxIndex]){
                if(maxIndex!=minIndex){
                    result+= prices[maxIndex] - prices[minIndex];
                }
                minIndex = i;
                maxIndex = i;
            }
        }
        if(prices[maxIndex]>prices[minIndex]){
            result+= prices[maxIndex] - prices[minIndex];
        }
        return result;
    }
}
